package com.tqjc.common.faulthandle.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tqjc.common.faulthandle.entity.FaultRecord;
import com.tqjc.common.faulthandle.service.FaultRecordService;
import com.tqjc.common.faulthandle.wrapper.FaultRecordWrapper;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.query.QueryUtils;
import com.tqjc.system.core.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;

/**
 * @projectName: BaotouSteel
 * @className: FaultRecordController
 * @author: Patienyon
 * @description: 故障记录控制器
 * @date: 2023-07-04 16:25
 */

@Controller
@RequestMapping("/faultRecord")
public class FaultRecordController {

    @Resource
    FaultRecordService faultRecordService;

    //辊道管理主页
    @RequestMapping("/index")
    public String index() {
        return "baotou-steel/NormalManage/FaultRecord.html";
    }

    /**
     * 获取表格数据 树形结构
     *
     * @param
     * @param request
     * @return
     */
    @RequestMapping("/datagrid")
    @ResponseBody
    public void datagrid(FaultRecord faultRecord, PageInfo<FaultRecord> page, HttpServletRequest request, HttpServletResponse response) {
        //拼接查询条件
        QueryWrapper<FaultRecord> queryWrapper = QueryUtils.installQueryWrapperRow(faultRecord, request.getParameterMap());

        //执行查询
        IPage<FaultRecord> lstResult = faultRecordService.page(page.getPageEntity(), queryWrapper);

        //结果处理,直接写到客户端
        ResponseUtil.write(response, new FaultRecordWrapper(lstResult).wrap());
    }


    //获取故障分类
    @RequestMapping("/getFaulttypeAndCountByParams")
    @ResponseBody
    public AjaxJson getFaulttypeAndCountByParams(@RequestParam(required = false) String craneType, @RequestParam(required = false) String workMode, @RequestParam(required = false) Timestamp beginTime, @RequestParam(required = false) Timestamp endTime, @RequestParam(required = false) String fuzzyMapper) {
        String message = null;
        try {
            Object o = faultRecordService.getFaultTypeAndCountByParams(craneType, workMode, beginTime, endTime, fuzzyMapper);
            return AjaxJson.ok(faultRecordService.getFaultTypeAndCountByParams(craneType, workMode, beginTime, endTime, fuzzyMapper), "获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            message = e.getMessage();
        }
        return AjaxJson.fail("获取失败" + message);
    }

}
